| enclose with | example |
|---|---|
| * or _ | italics |
| ** or __ | bold |
|nrow(mtcars)` |
|
| ^ | 102 |
| ~ | log10 |
[Rmd cheatsheet][https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf]
Add with one of the 3 ways:
print("I am the output of this code chunk.")
## [1] "I am the output of this code chunk."
chunk options
ggplot(mtcars, aes(x = cyl, y = mpg)) +
geom_point()
Be careful about caching + good for loading up computationally expensive output - not based on dependencies
data = mtcars[1:10, ]
proc = dplyr::filter(data, am == 1)
head(proc)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
data = mtcars[11:20, ]
proc = dplyr::filter(data, am == 1)
head(proc)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
defines default for every chunk
knitr::opts_chunk$set(echo = FALSE)
There are a total of 10 cars in the dataset with an average weight of 3.7 lbs
useful for re-rendering same report for different parameters
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
For the dataset above, you will find that it has 7 unique classes. To generate the same report for values from each class, params can be declared in the header.
## Output formats Rmarkdown can the generate the following document types: - pdf_document (requires LaTex) - word_document - odt_document - rtf_document - md_document - github_document
check parameters associated with specific outputs and change it by expanding output field
Add descriptive plot title
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Add subtitles and/or captions
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Replace axis or legend titles with labs()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
add math formulas with quote()
label individual observations or groups with geom_text()
highlight text by adding boxes with geom_label()
automatically adjust labels to prevent overlap using ggrepel pacakge and add layer to highlight points
replace legend with labels directly on plot
add text in plot border with Inf and wrap text using or stringr::str_wrap()
use vjust and hjust to adjust position of your labels or text
adjust your axis ticks for better visualisation, suppress labels with NULL
adjust breaks especially when you have sparse data points
use theme to adjust legend position
use guides() to control legend display
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Replace entire scale
adjusting color with RColorBrewer
use color-blind friendly theme
use scale_color_manual to use predefined mapping between values and colors
adjust continuous color scales with scale_color_gradient() or scale_fill_gradient() or viridis package
zoom into a plot region using coord_cartesian
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Adjust plot limits to allow comparison on the same scale
Expand scale of the 2nd plot so that it is comparable
by default, there are 8 themes in ggplot2, use ggthemes to add extra themes
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
use ggsave or knitr
## Saving 7 x 5 in image
adjust figure sizing with fig.width or out.width for output and add captio with fig.cap
cars plot